﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="NabaztagMessage.cs" company="Jim Liddell">
//   Copyright © Jim Liddell. All rights reserved.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace TeamBuildBunny
{
    #region Usings

    using System;
    using System.Text;
    using System.Web;

    #endregion

    /// <summary>
    /// A message that can be issued to a Nabaztag.
    /// </summary>
    public class NabaztagMessage
    {
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="NabaztagMessage"/> class.
        /// </summary>
        /// <param name="identity">
        /// The identity of the <see cref="Nabaztag"/>.
        /// </param>
        public NabaztagMessage(NabaztagIdentity identity)
        {
            this.Identity = identity;
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets the action.
        /// </summary>
        /// <remarks>
        /// See http://api.nabaztag.com/docs/home.html for further details.
        /// </remarks>
        public int Action { get; set; }

        /// <summary>
        /// Gets or sets the application ID.
        /// </summary>
        /// <remarks>
        /// The parameter is not used.
        /// </remarks>
        public int ApplicationId { get; set; }

        /// <summary>
        /// Gets or sets the name of the choreography.
        /// </summary>
        public string ChoreographyName { get; set; }

        /// <summary>
        /// Gets or sets a choreography string.
        /// </summary>
        public string ChoreographyString { get; set; }

        /// <summary>
        /// Gets the identity of the <see cref="Nabaztag"/>.
        /// </summary>
        public NabaztagIdentity Identity
        {
            get;
            private set;
        }

        /// <summary>
        /// Gets or sets the number of the message to send.
        /// </summary>
        /// <remarks>
        /// This number can refer to a message in the library or a personal MP3 file that you have downloaded. 
        /// You find this identification number under the title of the track you are listening to.
        /// </remarks>
        public int MessageId { get; set; }

        /// <summary>
        /// Gets or sets a text configured for speech synthesis.
        /// </summary>
        public string MessageText { get; set; }

        /// <summary>
        /// Gets or sets the Id of your nabcast.
        /// </summary>
        public int Nabcast { get; set; }

        /// <summary>
        /// Gets or sets the title of the post in your nabcast.
        /// </summary>
        public string NabcastTitle { get; set; }

        /// <summary>
        /// Gets or sets the position of the left ear between 0 and 16 (0 = ear vertical).
        /// </summary>
        public int PosLeft { get; set; }

        /// <summary>
        /// Gets or sets the position of the right ear between 0 and 16 (0 = ear vertical).
        /// </summary>
        public int PosRight { get; set; }

        /// <summary>
        /// Gets or sets a value indicating whether we are sending the position of the ears to the <see cref="Nabaztag"/>.
        /// </summary>
        public bool SendEars { get; set; }

        /// <summary>
        /// Gets or sets the length of time you want a message to remain on the site (in seconds).
        /// </summary>
        /// <remarks>
        /// By default, the message will be stored for a period of two months.
        /// </remarks>
        public int TimeToLiveSeconds { get; set; }

        #endregion

        #region Public Methods

        /// <summary>
        /// Returns a string representation of the <see cref="NabaztagMessage"/>.
        /// </summary>
        /// <returns>
        /// A string representation of the <see cref="NabaztagMessage"/>.
        /// </returns>
        public override string ToString()
        {
            StringBuilder request = new StringBuilder();
            request.AppendFormat("sn={0}&token={1}", this.Identity.SerialNumber, this.Identity.Token);
            if (this.TimeToLiveSeconds > 0)
            {
                request.AppendFormat("&ttlive={0}", this.TimeToLiveSeconds);
            }

            if (this.Action > 0)
            {
                request.AppendFormat("&action={0}", this.Action);
            }

            if (this.MessageId > 0)
            {
                request.AppendFormat("&idmessage={0}", this.MessageId);
            }

            if (this.Nabcast > 0)
            {
                request.AppendFormat("&nabcast={0}", this.Nabcast);
            }

            if (!String.IsNullOrEmpty(this.NabcastTitle))
            {
                request.AppendFormat("&nabcasttitle={0}", HttpUtility.UrlEncode(this.NabcastTitle));
            }

            if (this.SendEars)
            {
                request.AppendFormat("&posright={0}&posleft={1}&ears=ok", this.PosRight, this.PosLeft);
            }

            if (!String.IsNullOrEmpty(this.ChoreographyName))
            {
                request.AppendFormat("&chortitle={0}", HttpUtility.UrlEncode(this.ChoreographyName));
            }

            if (!String.IsNullOrEmpty(this.ChoreographyString))
            {
                request.AppendFormat("&chor={0}", this.ChoreographyString);
            }

            if (!String.IsNullOrEmpty(this.Identity.Voice.Command))
            {
                request.AppendFormat("&voice={0}", this.Identity.Voice.Command);
            }

            if (!String.IsNullOrEmpty(this.MessageText))
            {
                request.AppendFormat("&tts={0}", HttpUtility.UrlEncode(this.MessageText));
            }

            return request.ToString();
        }

        #endregion
    }
}